<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="./lib/bootstrap.css">
</head>

<body>
    <div style='padding: 10px 30px'>
        <!-- 评论面板 -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">发表评论</h3>
            </div>
            <form class="panel-body" id="formAddCmt">
                <div>评论人：</div>
                <input type="text" class="form-control" name="username" autocomplete="off" />
                <div>评论内容：</div>
                <textarea class="form-control" name="content"></textarea>
                <button type="submit" class="btn btn-primary">发表评论</button>
            </form>
        </div>
        <!-- 评论列表 -->
        <ul class="list-group" id="cmt-list">
            <!-- <li class="list-group-item">
        <span class="badge" style="background-color: #F0AD4E;">评论时间：</span>
        <span class="badge" style="background-color: #5BC0DE;">评论人：</span>
        Item 1
      </li> -->
        </ul>
    </div>
    <script type="text/javascript" src="./lib/jquery.js"></script>
    <script src="./lib/template-web.js"></script>
    <script type="text/html" id="aaa">
        {{each data}}
        <li class="list-group-item">
            <span class="badge" style="background-color: #F0AD4E;">评论时间：{{$value.time}}</span>
            <span class="badge" style="background-color: #5BC0DE;">评论人：{{$value.username}}</span> {{$value.content}}
        </li>
        {{/each}}
    </script>
    <script type="text/javascript">
        $(function() {
            // 加载评论区数据
            function loadCommentList() {
                // 获取服务器信息
                $.get('http://www.liulongbin.top:3006/api/cmtlist', function(res) {
                    console.log(res);
                    var list = res.data;
                    var ret = template('aaa', res)
                        // var arr = [];
                        // list.forEach(function(item) {
                        //         var lis = `
                        // <li class="list-group-item">
                        //     <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
                        //     <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
                        //     ${item.content}
                        // </li>
                        // `
                        //         arr.push(lis)
                        //     })
                        //     // 将信息写进ul
                        // $('ul').eq(0).html(arr.join(''))
                    $('ul').eq(0).html(ret)
                })
            }
            loadCommentList();
            $('form').submit(function(e) {
                e.preventDefault();
                // console.log(this)
                var formdata = $(this).serialize();
                $.post('http://www.liulongbin.top:3006/api/addcmt', formdata, function(res) {
                    // console.log(res)
                    if (res.status == 201) {
                        loadCommentList();
                        $('.form-control').val('')
                    }
                })
            })
        })
    </script>
</body>

</html>